package com.rabbitmq.ChattingRoom.controller.biz;

import com.chattingroom.mqproducer.mqproducer.feign.interfaces.MyProducerFeign;
import com.rabbitmq.ChattingRoom.service.HistoryMsgService;
import com.rabbitmq.ChattingRoom.util.Entity.HistoryMsg;
import com.rabbitmq.ChattingRoom.util.common.base.Result;
import com.rabbitmq.ChattingRoom.util.common.req.*;
import com.rabbitmq.ChattingRoom.util.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class ChattingBiz {

    @Autowired
    private MyProducerFeign myProducerFeign;
    @Autowired
    private HistoryMsgService historyMsgService;

    public Result<String> sendMessage(SendMessageToQueueReq req) {
        System.err.println(req);
        if (myProducerFeign.sendMessageToQueue(req)) {
            // 将消息存储到数据库
            HistoryMsg historyMsg = BeanUtil.copyProperties(req.getMessage(), HistoryMsg.class);
            historyMsg.setId(Long.parseLong(req.getMessage().getId()))
                    .setFromId(Long.parseLong(req.getMessage().getFromId()))
                    .setReveId(Long.parseLong(req.getMessage().getReveId()))
            ;
            historyMsgService.insert(historyMsg);
            return Result.success("消息发送成功!");
        }
        return Result.error("消息发送失败!");
    }


}
